<?php
/**
 *  CMS 文章管理
 *  仲伟涛 2011-4-24
 */
class Admin_Page_Cms_Document extends Admin_Page_Abstract
{
    public function validate(ME_Request $input, ME_Response $output)
    {
        $input->doJump = true;
        $output->adminInfo  =
        $input->adminInfo   = Libs_UcUser::getAdminLoginInfo($input,$output);
        
        $appId              = (int)$input->get("appId");
        if(!$appId)  $appId = (int)$input->post("appId");
        $output->appId      =    $input->appId    = $appId;
        
        return true;
    }

    private $db;
    private $tblTitleName = 'cms_document_title_';
    private $tblContentName = 'cms_document_content_';

    private function loadDb()
    {
        $this->db = Db_Admin::instance();
    }


    public function doDefault(ME_Request $input, ME_Response $output)
    {
        $page = (int)$input->get("page");
        if(!$input->appId){
            echo "app 参数错误!";
            exit;
        }
        $page = max(1,$page);
        $this->loadDb();
        //获得搜索条件
        $output->sel =
        $selOption = $input->get('sel');
        $subWhere = '';
        if($selOption['classId']){
            $subWhere .= " AND classId = ".$selOption['classId']." ";
        }
        if($selOption['status'] != '-1' && ($selOption['status'] || $selOption['status']==='0')){
            $subWhere .= " AND status = ".$selOption['status']." ";
        }
        if($selOption['keyword'] ){
            $subWhere .= " AND title like '%".$selOption['keyword']."%' ";
        }
        if($selOption['enfile'] ){
            $subWhere .= " AND enfile = '".$selOption['enfile']."' ";
        }

        $subWhere = $subWhere ? ' WHERE 1 '.$subWhere : '';
        //echo $subWhere;exit;
        //获得所有记录
        $param = array(
                'db'          => $this->db,
                'fromSql'     => " from {$this->tblTitleName}{$input->appId} ", #from部分的语句
                'orderBySql'  => ' order by id desc ',
                'whereSql'  => $subWhere,
                'pagesize'  => 20,
                'page'      => $page,
                'urlTpl'    => 'index.php?c='.$input->ctlName.'&appId='.$input->appId.'&sel[classId]='.$selOption['classId'].'&sel[status]='.$selOption['status'].'&page={PAGE}',
        );
        $listData = Libs_DBHelper::getList($param);
        $output->pageBar = $listData['pageBar'];
        $output->list = $listData['list'];
        #顶部
        $output->headerHtml = Admin_Plugin_Common_Tpl::getHeader($input, $output);
        //APP缓存
        $output->appCache = $this->loadCache("Global_App",array());
        //分类缓存
        $output->classCache = $this->loadCache("Cms_Class",array('appId' => $input->appId));

        $output->setTemplate("Cms/DocumentList");
    }
    public function doAddShow(ME_Request $input, ME_Response $output)
    {
        $id = (int)$input->get("id");
        if($id){
            $this->loadDb();
            $sql = "select * from {$this->tblTitleName}{$input->appId} where id=".$id;
            $output->dataInfo = $this->db->getRow($sql);
            $contentSql =  "select content from {$this->tblContentName}{$input->appId} where documentId={$id}";
            $output->content = $this->db->getOne($contentSql);

            $output->id = $id;
        }

        //APP缓存
        $output->appCache = $this->loadCache("Global_App",array());
        #顶部
        $output->headerHtml = Admin_Plugin_Common_Tpl::getHeader($input, $output);
        //分类缓存
        $output->classCache = $this->loadCache("Cms_Class",array('appId' => $input->appId));
        $output->setTemplate("Cms/DocumentAdd");
    }
    public function doAdd(ME_Request $input, ME_Response $output)
    {        
        $this->loadDb();
        $input->id =
        $id = (int)$input->post("id");
        $item = $input->post("item");
        $item['datetm'] = SYSTEM_TIME;
        $item['username'] = $input->adminInfo[0];
        
        $options = array(
                'db'        => $this->db,
                'tableName' => $this->tblTitleName . $input->appId,
                'addItem'   => $item,
            );
        if($id){//编辑
            $options['addItem']['status'] = 0;
            $options['where'] = ' where id = '.$id;
            Libs_DBHelper::updateItem($options);
        }else{
            $id = Libs_DBHelper::insertItem($options);
        }
        
        //内容部分
        $content = $input->post("content",true);
        $item2 = array(
            'documentId' => $id,
            'content'    => $content,
        );
        $options2 = array(
                        'db'        => $this->db,
                        'tableName' => $this->tblContentName . $input->appId,
                        'addItem'   => $item2,
                        'replace'   => 1,
                       // 'debug'     => 1,
                    );
       Libs_DBHelper::insertItem($options2);       

       //检查标题和文件名是否相同
       $sql = "select 'X' from cms_document_title_1 where status = 3 and ( title = '".$item["title"]."'";
       if($item["enfile"])$sql .= " or enfile ='".$item["enfile"]."'";
       $sql .= ")";

       $hasSame = $this->db->getOne($sql);
       if($hasSame){
            Libs_GlobalFunc::haltMsg("有相同标题或者的文章!!!",array('stop'=>true,'type'=>'failure'));
       }
       //判断是否直接发布文章
       $quickPut = (int)$input->post('quickPub');
       if($quickPut){
           $result = $this->publishDocument($input, $output);
           Libs_GlobalFunc::haltMsg("文章编辑成功！",array('backUrl'=>$result['url']));
       }
       Libs_GlobalFunc::haltMsg("文章编辑成功！");

    }
    //彻底删除
    public function doRelDel(ME_Request $input, ME_Response $output)
    {
        $did = (int)$input->get("id");
        if(!$did){
            echo '参数错误!<a href="javascript:;" onclick="javascript:history.go(-1)">返回</a>';
            exit;
        }
        $this->loadDb();
        Libs_Cms_Document::publishDocument(array('output'=> $output,'appId' => $input->appId , 'docId'=>$did ,'set404'=>true));
        
        $sql = "delete from {$this->tblTitleName}{$input->appId} where id={$did}";
        $this->db->query($sql);
        $sql = "delete from {$this->tblContentName}{$input->appId} where documentId={$did}";
        $this->db->query($sql);
        Libs_GlobalFunc::haltMsg("彻底删除成功！");
    }

    //删除文件
    public function doDel(ME_Request $input, ME_Response $output)
    {
        $did = (int)$input->get("id");
        if(!$did){
            Libs_GlobalFunc::haltMsg("请选择文章！");
        }
        $this->loadDb();
        $sql = "update {$this->tblTitleName}{$input->appId} set status = 1 where id={$did}";
        $this->db->query($sql);
        Libs_Cms_Document::publishDocument(array('output'=> $output,'appId' => $input->appId , 'docId'=>$did ,'set404'=>true));
        Libs_GlobalFunc::haltMsg("删除成功！");
    }

    //恢复删除
    public function doRestore(ME_Request $input, ME_Response $output)
    {
        $did = (int)$input->get("id");
        if(!$did){
            Libs_GlobalFunc::haltMsg("请选择文章！");
        }
        $this->loadDb();
        $sql = "update {$this->tblTitleName}{$input->appId} set status = 0 where id={$did}";
        $this->db->query($sql);
        Libs_GlobalFunc::haltMsg("恢复成功！");
    }
    public function doMove(ME_Request $input, ME_Response $output){
        $toClassid = $input->get("toClassid");
        $ids = $input->post("ids");
        $appId    = $input->post("appId");

        if($appId && $ids && $toClassid){
            $this->loadDb();
            $idsStr = implode(",",$ids);
            $sql = "update {$this->tblTitleName}{$appId} set classId={$toClassid} where id in ($idsStr)";
            $this->db->query($sql);
            Libs_GlobalFunc::haltMsg("转移成功！");
        }else{
            echo "参数错误!";
        }
    }
    public function doPublish(ME_Request $input, ME_Response $output)
    {
        $input->id = (int)$input->get("id");
        if(!$input->id){
            echo '参数错误!<a href="javascript:;" onclick="javascript:history.go(-1)">返回</a>';
            exit;
        }
        $result = $this->publishDocument($input, $output);

        /*$this->loadDb();
        $result = Libs_Cms_Document::publishDocument(array('output'=> $output,'appId' => $input->appId , 'docId'=>$did));
        //更新发布状态
        $sql = "update {$this->tblTitleName}{$input->appId} set status = 3 where id={$did}";
        $this->db->query($sql);*/

        Libs_GlobalFunc::haltMsg("文章发布成功！",array('backUrl'=>$result['url']));

    }
    private function publishDocument(ME_Request $input, ME_Response $output){
        $this->loadDb();
        $result = Libs_Cms_Document::publishDocument(array('output'=> $output,'appId' => $input->appId , 'docId'=>$input->id));
        //更新发布状态
        $sql = "update {$this->tblTitleName}{$input->appId} set status = 3 where id={$input->id}";
        $this->db->query($sql);
        return $result;

    }

}
